package com.huawei.hianalytics.f.f;

import android.content.Context;
import com.huawei.hianalytics.f.b.a;
import com.huawei.hianalytics.f.b.c;
import com.huawei.hianalytics.f.b.d;
import com.huawei.hianalytics.f.g.b;
import com.huawei.hianalytics.f.g.h;
import com.huawei.hianalytics.f.g.j;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;

class g implements h {
    private Context a;
    private String b;
    private String c;
    private a[] d;
    private String e;

    g(Context context, a[] aVarArr, String str, String str2, String str3) {
        this.a = context;
        this.b = str;
        this.c = str2;
        this.d = (a[]) aVarArr.clone();
        this.e = str3;
    }

    private void a() {
        b.a().a(j.a(this.b, this.c, this.e));
    }

    private void a(a[] aVarArr, String str) {
        JSONArray jSONArray = new JSONArray();
        long length = h.c(this.a, "cached_v2_1").length();
        int length2 = aVarArr.length;
        int i = 0;
        while (i < length2) {
            a aVar = aVarArr[i];
            c aVar2 = new a(this.a);
            aVar.a(aVar2);
            JSONObject a = aVar2.a(true, true);
            length += aVar2.i();
            if (length > ((long) com.huawei.hianalytics.a.b.m()) * 1048576) {
                com.huawei.hianalytics.g.b.c("EventSendResultHandleTask", "failed data length is too big! length: " + length);
                break;
            } else {
                jSONArray.put(a);
                i++;
            }
        }
        try {
            h.a(this.a, jSONArray.toString(), "cached_v2_1", str);
        } catch (OutOfMemoryError e) {
            com.huawei.hianalytics.g.b.c("EventSendResultHandleTask", "saveEventInfo: jsonArray.toString ->OOM,delete failed data");
        }
    }

    public void run() {
        if (this.d == null || this.d.length <= 0) {
            com.huawei.hianalytics.g.b.b("EventSendResultHandleTask", "No cache info save! reqID:" + this.e);
        } else {
            String str = !"_default_config_tag".equals(this.b) ? this.b + "-" + this.c : "_default_config_tag";
            int m = com.huawei.hianalytics.a.b.m();
            com.huawei.hianalytics.g.b.b("HiAnalytics/event", "data send failed, write to cache file...reqID:" + this.e);
            if (h.a(this.a, "cached_v2_1", m * 1048576)) {
                com.huawei.hianalytics.g.b.c("EventSendResultHandleTask", "THe cacheFile is full,Not writing data! reqID:" + this.e);
                return;
            }
            int size;
            c[] a = a.a(this.a, str);
            int length = this.d.length;
            List arrayList = new ArrayList();
            if (a.length > 0) {
                List subList;
                List a2 = o.a(a);
                size = a2.size() + length;
                if (size > 6000) {
                    size = 6000;
                    subList = a2.subList(length, 6000);
                } else {
                    subList = a2;
                }
                for (length = 0; length < subList.size(); length++) {
                    arrayList.add(((d) subList.get(length)).a());
                }
            } else {
                size = length;
            }
            a[] aVarArr = (a[]) arrayList.toArray(new a[arrayList.size()]);
            Object obj = new a[size];
            System.arraycopy(this.d, 0, obj, 0, this.d.length);
            if (aVarArr.length > 0) {
                System.arraycopy(aVarArr, 0, obj, this.d.length, aVarArr.length);
            }
            a(obj, str);
        }
        a();
    }
}
